package com.commonsware.android.maps;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class GalleryView extends Activity {
	 Integer[] picsTunis = {
	    		R.drawable.a,
	    		R.drawable.b,
	    		R.drawable.c,
	    		R.drawable.d,
	    		R.drawable.e,

	    };
	 Integer[] picsTunis2 = {
	    		R.drawable.antartica1,
	    		R.drawable.antartica2,
	    		R.drawable.antartica3,
	    		
	    };
	 Integer[] picsGasserine = {
	    		R.drawable.antartica6,
	    		R.drawable.antartica7,
	    		R.drawable.antartica8,
	    		R.drawable.antartica9,

	    };
    Integer[] picsSidiBouzid = {
    		R.drawable.antartica1,
    		R.drawable.antartica2,
    		R.drawable.antartica3,
    		R.drawable.antartica4,
    		
    };
    Integer[] pics;
    ImageView imageView;
    String albumEtat;
    
   
	/** Called when the activity is first created. */
    

    public void onCreate(Bundle savedInstanceState) {
    	Intent i = getIntent();
        String foosh = i.getStringExtra("SNARP");
        albumEtat=foosh;
    	 if(foosh.compareTo("SidiBouzid")==0)
    	    	pics=picsSidiBouzid;
    	    else if(foosh.compareTo("Gasserine")==0)
    	    	pics=picsGasserine;
    	    else if(foosh.compareTo("Tunis")==0)
    	    	pics=picsTunis;
    	    else
    	    	pics=picsTunis2;
       // pics=picsTunis;
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main11);
        
        Gallery ga = (Gallery)findViewById(R.id.Gallery01);
        ga.setAdapter(new ImageAdapter(this));
        
        imageView = (ImageView)findViewById(R.id.ImageView01);
        ga.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				/*Toast.makeText(getBaseContext(), 
						"You have selected picture " + (arg2+1) , 
						Toast.LENGTH_SHORT).show();*/
				//imageView.setImageResource(pics[arg2]);
				Intent intent = new Intent(GalleryView.this,detailimage.class);
    		//	startActivityForResult(intent, 0);

    			
              //  setResult(RESULT_OK, intent);
    			Bundle b = new Bundle();
    			String album=albumEtat;	
    			String num=""+arg2;		

    			b.putString("album", album);
    		b.putString("num", num);


    			intent.putExtras(b);
                startActivityForResult(intent, 0);
			}
        	
        });
        
    }
    
    
    public class ImageAdapter extends BaseAdapter {

    	private Context ctx;
    	int imageBackground;
    	
    	public ImageAdapter(Context c) {
			ctx = c;
			TypedArray ta = obtainStyledAttributes(R.styleable.Gallery1);
			imageBackground = ta.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 1);
			ta.recycle();
		}

		@Override
    	public int getCount() {
    		
    		return pics.length;
    	}

    	@Override
    	public Object getItem(int arg0) {
    		
    		return arg0;
    	}

    	@Override
    	public long getItemId(int arg0) {
    		
    		return arg0;
    	}

    	@Override
    	public View getView(int arg0, View arg1, ViewGroup arg2) {
    		ImageView iv = new ImageView(ctx);
    		iv.setImageResource(pics[arg0]);
    		iv.setScaleType(ImageView.ScaleType.FIT_XY);
    		iv.setLayoutParams(new Gallery.LayoutParams(Gallery.LayoutParams.FILL_PARENT,Gallery.LayoutParams.FILL_PARENT));
    		
    		
    		iv.setBackgroundResource(imageBackground);
    		return iv;
    	}

    }
}